﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTQT;
using TTQT.TTQT_Code;
using MyWebUtils;

public partial class HoaDonTrongNgay : MasterControl //System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtNgay.Value = DateTime.Now.ToString("dd/MM/yyyy");
            bindMaster();
            hienPanel(true, false);
        }
    }
    private string tinhTongTien(string macn, string userName, string group, string ngay)
    {
        string kq = string.Empty;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("HoaDon_TrongNgay_Sum", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@macn", UserInfo.MaKho);
        cmd.Parameters.AddWithValue("@userName", UserInfo.userName);

        cmd.Parameters.AddWithValue("@group", group);
        cmd.Parameters.AddWithValue("@ngay", ngay);
        try
        {
            conn.Open();
            kq = cmd.ExecuteScalar().ToString();
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return TTQTCommon.formatTien_KhongThapPhan(kq);



        //ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        //ds.SelectCommand = "HoaDon_TrongNgay_Sum";
        //ds.SelectParameters.Clear();

        //ds.SelectParameters.Add(new Parameter("macn"));
        //ds.SelectParameters["macn"].DefaultValue = UserInfo.MaKho;

        //ds.SelectParameters.Add(new Parameter("userName"));
        //ds.SelectParameters["userName"].DefaultValue = UserInfo.userName;

        //ds.SelectParameters.Add(new Parameter("group"));
        //ds.SelectParameters["group"].DefaultValue = group;

        //ds.SelectParameters.Add(new Parameter("ngay"));
        //ds.SelectParameters["ngay"].DefaultValue = ngay;

        //try
        //{
        //    DataView hd = (DataView)dsMaster.Select(System.Web.UI.DataSourceSelectArguments.Empty);
        //    //masterGrid.DataSource = hd;
        //    kq= TTQT.TTQT_Code.TTQTCommon.formatTien_KhongThapPhan(hd.ToTable().Rows[0]["TongTien"].ToString());
        //}
        //catch (Exception exx)
        //{ kq = "..."; }
        //return kq;
    }
    private void bindMaster()
    {
        lblSoLuongHoaDon.Text = string.Empty;
        string group = "gdv";
        if (isRole("gdv"))
        {
            group = "gdv";
        }
        else if (isRole("adminCN"))
        {
            group = "adminCN";
        }
        String ngay = DateTime.Now.ToString("dd/MM/yyyy");
        try
        {
            DateTime d = Convert.ToDateTime(txtNgay.Value);
            ngay = txtNgay.Value;
        }
        catch (Exception ex)
        {

        }

        dsMaster.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        dsMaster.SelectCommand = "HoaDon_TrongNgay";
        dsMaster.SelectParameters.Clear();

        dsMaster.SelectParameters.Add(new Parameter("macn"));
        dsMaster.SelectParameters["macn"].DefaultValue = UserInfo.MaKho;

        dsMaster.SelectParameters.Add(new Parameter("userName"));
        dsMaster.SelectParameters["userName"].DefaultValue = UserInfo.userName;

        dsMaster.SelectParameters.Add(new Parameter("group"));
        dsMaster.SelectParameters["group"].DefaultValue = group;

        dsMaster.SelectParameters.Add(new Parameter("ngay"));
        dsMaster.SelectParameters["ngay"].DefaultValue = TTQTCommon.convertDateString(ngay);
        string tongTien = tinhTongTien(UserInfo.MaKho, UserInfo.userName, group, TTQTCommon.convertDateString(ngay));
        try
        {
            DataView hd = (DataView)dsMaster.Select(System.Web.UI.DataSourceSelectArguments.Empty);
            masterGrid.DataSource = hd;
            //this.masterGrid.DataSourceID = "dsMaster";
            masterGrid.DataBind();
            lblSoLuongHoaDon.Text = "<b><span style='color:blue;'>Tổng số hóa đơn:</span><span style='color:red;'>" + hd.Count + "</span><span style='color:blue;'>. Tổng số tiền:</span> <span style='color:red;'>" + tongTien + "</span><span style='color:blue;'> VND</span></b>";
        }
        catch (Exception exx)
        {
            lblSoLuongHoaDon.Text = "<br /><span style='color:blue;'><b>Không có hóa đơn nào.</b></span>";
        }


    }
    private void hienPanel(bool gridMaster, bool detail)
    {
        pnGridMaster.Visible = gridMaster;
    }
    protected void masterGrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "cmdDetail")
            {

            }

        }
        catch (SqlException ex)
        {
        }
    }


    protected void masterGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        masterGrid.CurrentPageIndex = e.NewPageIndex;
        masterGrid.DataBind();
    }
    protected void cmdXem_Click(object sender, EventArgs e)
    {
        bindMaster();
    }
    protected void masterGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        
    }
}